package com.eccg.shopping.activity;


import java.util.ArrayList;
import java.util.Collections;

import com.eccg.shopping.R;
import com.eccg.shopping.utils.MyComparator;
import com.eccg.shopping.utils.Pinyin4j;
import com.eccg.shopping.utils.SideBar;
import com.eccg.shopping.utils.Store;
import com.eccg.shopping.utils.StoreAdapter;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class StoreSelectActivity extends BaseActivity{

	private ListView storeselect_lv;
	private String a2z = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

	private ArrayList<String> list = new ArrayList<String>();
	private ArrayList<Store> storesList = new ArrayList<Store>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		list = getList();
		storesList = getStoreList(list);
		this.setContentView(R.layout.store_select);
	
		storeselect_lv = (ListView) findViewById(R.storeselect.storeselect_lv);

		final StoreAdapter adapter = new StoreAdapter(this, storesList);
		storeselect_lv.setAdapter(adapter);
		storeselect_lv.setDivider(null);
		storeselect_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Store store = (Store) storeselect_lv.getItemAtPosition(position);
				Intent intent = new Intent();
				intent.setClass(StoreSelectActivity.this, StoreActivity.class);
				Bundle bundle = new Bundle();
				bundle.putParcelable("singlestore", store);
				intent.putExtras(bundle);
				StoreSelectActivity.this.startActivity(intent);
			}
		});
		
		SideBar sideBar = (SideBar) findViewById(R.store_select.store_select_sidebar);
		sideBar.setListView(storeselect_lv);
	}

	@SuppressWarnings("unchecked")
	public ArrayList<Store> getStoreList(ArrayList<String> list) {
		ArrayList<Store> storeList = new ArrayList<Store>();
		for (int i = 0; i < list.size(); i++) {
			String storeName = list.get(i);
			Store store = new Store(list.get(i), Pinyin4j.getPinyin(storeName));
			if(Pinyin4j.getPinyin(storeName) != null && !"".equals(Pinyin4j.getPinyin(storeName)) && a2z.contains(Pinyin4j.getPinyin(storeName).substring(0, 1))){
				storeList.add(store);
			}
		}
		storesList = storeList;
		
		String[] sortBy = new String[] { "storePinyin" };
		int orderBy = 1;// 1:升序，-1：降序
		MyComparator myCmp = new MyComparator(sortBy, orderBy);
		Collections.sort(storesList, myCmp);
		
		for (int i = 0; i < list.size(); i++) {
			String storeName = list.get(i);
			Store store = new Store(list.get(i), Pinyin4j.getPinyin(storeName));
			if(Pinyin4j.getPinyin(storeName) == null || "".equals(Pinyin4j.getPinyin(storeName)) || !a2z.contains(Pinyin4j.getPinyin(storeName).substring(0, 1))){
				storesList.add(store);
			}
		}
		
		return storesList;
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	private ArrayList<String> getList() {
		list = new ArrayList<String>();
		list.add("Vancl");
		list.add("BRT");
		list.add("Adidas");
		list.add("Uniqlo");
		list.add("聚仙阁");
		list.add("G.SB");
		list.add("同仁堂");
		list.add("Jeanswest");
		list.add("安踏");
		list.add("Artka");
		list.add("汇成丰");
		list.add("雅蝶娜");
		list.add("吉善缘");
		list.add("三合");
		list.add("Coocolor");
		list.add("韩都衣舍");
		list.add("Camel");
		list.add("卡玛");
		list.add("风水阁");
		list.add("Jack Jones");
		list.add("鸿星尔克");
		list.add("雅轩斋");
		list.add("香影");
		list.add("Deezu");
		list.add("欢畅");
		list.add("Bay Eagle Eyes");
		list.add("双枪");
		return list;
	}

	@Override
	protected int getMenuIndex() {
		return 0;
	}
	
	
	@Override
	protected void initHeader() {
		// TODO Auto-generated method stub
		super.initHeader();
		titleTx.setText("品牌列表");
		Button backBtn = new Button(this);
		backBtn.setText("返回");
		backBtn.setTextSize(16);
		backBtn.setTextColor(getResources().getColor(R.color.white));
		backBtn.setGravity(Gravity.CENTER);
		backBtn.setBackgroundResource(R.drawable.left_btn_bg);
		backBtn.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		leftTitle.removeAllViews();
		leftTitle.addView(backBtn);
	}
}
